#import "CTBaseModel.h"
@implementation CTBaseModel
+ (void)modelWithUrlString:(NSString *)urlString imageView:(UIImageView *)imageView {
    NSURL *url = [NSURL URLWithString:urlString];
    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"sh_placeholder_logo"]];
}
- (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property
{
    if (property.type.numberType == YES) {
        if (oldValue == nil) {
            oldValue = @(0);
        }
    }
    else if(property.type.typeClass == [NSString class])
    {
        if (oldValue == nil) {
            oldValue = @"";
        }
        if(oldValue && oldValue == [NSNull null]) {
            oldValue = @"";
        }
    }
    return oldValue;
}
+ (NSMutableArray *)modelArrayWithDictArray:(NSArray *)response {
    if ([response isKindOfClass:[NSArray class]]) {
        NSMutableArray *array = [self mj_objectArrayWithKeyValuesArray:response];
        return array;
    }
    return [NSMutableArray new];
}
+ (id)modelWithDictionary:(NSDictionary *)dictionary {
    if ([dictionary isKindOfClass:[NSDictionary class]]) {
        return [self mj_objectWithKeyValues:dictionary];
    }
    return [[self alloc] init];
}
+ (void)setUpModelClassInArrayWithContainDict:(NSDictionary *)dict {
    if (dict.allKeys.count == 0) {
        return ;
    }
    [self mj_setupObjectClassInArray:^NSDictionary *{
        return dict;
    }];
}
+ (NSMutableArray *)modelArrayWithDictArray:(NSArray *)response containDict:(NSDictionary *)dict {
    if (dict == nil) {
        dict = [NSMutableDictionary new];
    }
    [self setUpModelClassInArrayWithContainDict:dict];
    return [self modelArrayWithDictArray:response];
}
+ (id)modelWithDictionary:(NSDictionary *)dictionary containDict:(NSDictionary *)dict {
    if (dict == nil) {
        dict = [NSMutableDictionary new];
    }
    [self setUpModelClassInArrayWithContainDict:dict];
    return [self modelWithDictionary:dictionary];
}
- (NSMutableDictionary *)model_keyValues
{
    return [self mj_keyValues];
}
- (CGFloat)getSizeOfString:(NSString *)string font:(UIFont *)font width:(float)width
{
    CGSize size = CGSizeZero;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
    size = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:
            NSStringDrawingUsesFontLeading |
            NSStringDrawingUsesLineFragmentOrigin
                             attributes:attributes context:nil].size;
    return size.height;
}
- (CGFloat)getSizeOfString:(NSString *)string lineSpace:(CGFloat)lineSpace font:(UIFont *)font width:(float)width
{
    CGSize size = CGSizeZero;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.lineSpacing = lineSpace;
    NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
    size = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:
            NSStringDrawingUsesFontLeading |
            NSStringDrawingUsesLineFragmentOrigin
                             attributes:attributes context:nil].size;
    return size.height;
}
#pragma mark - cache
- (NSString *)debugDescription {
    if ([self isKindOfClass:[NSArray class]] || [self isKindOfClass:[NSDictionary class]] || [self isKindOfClass:[NSString class]] || [self isKindOfClass:[NSNumber class]]) {
        return [self debugDescription];
    }
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    uint count;
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    for (int i = 0; i<count; i++) {
        objc_property_t property = properties[i];
        NSString *name = @(property_getName(property));
        id value = [self valueForKey:name]?:@"nil";
        [dictionary setObject:value forKey:name];
    }
    free(properties);
    return [NSString stringWithFormat:@"<%@: %p> -- %@",[self class],self,dictionary];
}
@end
